// capitalDir  0:  付款   1: 收款    2:  无金额

/**
 *求和
 * @param arg1 参数1
 * @param arg2 参数2
 * @param y 保留几位小数
 * @returns
 */
function float_add(arg1, arg2, y?: number) {
  var r1, r2, m, n;
  try {
    r1 = arg1.toString().split('.')[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = arg2.toString().split('.')[1].length;
  } catch (e) {
    r2 = 0;
  }
  m = Math.pow(10, Math.max(r1, r2));
  n = r1 >= r2 ? r1 : r2;
  if (y) n = y;
  return ((arg1 * m + arg2 * m) / m).toFixed(n);
}

/** 求和 */
const summation = (dataList: Record<string, any>) => {
  let count: number | string = 0;
  dataList?.forEach((item: any) => {
    // count += item?.value ? item?.value : 0;
    count = float_add(count, item?.value ? item?.value : 0);
  });
  return count;
};

/**
 * 合同汇总统计
 * @param info 合同数据
 */
const stateCountContract = (info: Record<string, any>, loadInfo: Record<string, any>) => {
  const payInfo: Record<string, any> = info?.['0']; //付款数据
  const collecInfo: Record<string, any> = info?.['1']; //收款数据
  let payList: Record<string, any>[] = []; //付款数据集合
  let collecList: Record<string, any>[] = []; //付款数据集合

  // // 遍历含月份的付款数据
  if (payInfo) {
    Object.values(payInfo)?.forEach((itemPay: any) => {
      payList = payList?.concat(itemPay);
    });
  }

  // // 遍历含月份的收款数据
  if (collecInfo) {
    Object.values(collecInfo)?.forEach((itemCollect: any) => {
      collecList = collecList?.concat(itemCollect);
    });
  }

  let topStateNameList: string[] = []; //状态名称集合

  let topStatePayNums: number[] = []; // 上方状态付款数量
  let topStatePaySums: number[] = []; //上方状态付款金额
  let topStatePutNums: number[] = []; //上方收款数量
  let topStatePutSums: number[] = []; //上方状态收款金额

  let payNumsInfoList: Record<string, any>[] = []; //付款数量结合
  let paySumsInfoList: Record<string, any>[] = []; //付款金额结合
  let putNumsInfoList: Record<string, any>[] = []; //收款金额结合
  let putSumsInfoList: Record<string, any>[] = []; //收款金额结合

  //遍历数据字典
  Object?.keys(loadInfo)?.forEach((itemState: any) => {
    //遍历数据字典
    topStateNameList?.push(loadInfo[itemState]?.name);

    let a_topStatePayNums: number = 0; //付款数量
    let a_topStatePaySums: number = 0; //付款金额
    let b_topStatePutNums: number = 0; //收款数量
    let b_topStatePutSums: number = 0; //收款金额

    payList?.forEach((itemPay: any) => {
      if (loadInfo[itemState]?.id == itemPay?.state) {
        a_topStatePayNums += itemPay?.totalNum;
        a_topStatePaySums += itemPay?.totalSum;
      }
    });

    //付款饼图数据
    payNumsInfoList?.push({
      name: loadInfo[itemState]?.name,
      value: a_topStatePayNums,
    });
    paySumsInfoList?.push({
      name: loadInfo[itemState]?.name,
      value: a_topStatePaySums,
    });

    topStatePayNums?.push(a_topStatePayNums);
    topStatePaySums?.push(a_topStatePaySums);

    collecList?.forEach((itemPut: any) => {
      if (loadInfo[itemState]?.id == itemPut?.state) {
        b_topStatePutNums += itemPut?.totalNum;
        b_topStatePutSums += itemPut?.totalSum;
      }
    });

    //收款合同数据
    putNumsInfoList?.push({
      name: loadInfo[itemState]?.name,
      value: b_topStatePutNums,
    });
    putSumsInfoList?.push({
      name: loadInfo[itemState]?.name,
      value: b_topStatePutSums,
    });

    topStatePutNums?.push(b_topStatePutNums);
    topStatePutSums?.push(b_topStatePutSums);
  });

  const payNumAll: any = summation(payNumsInfoList);
  const paySumAll: any = summation(paySumsInfoList);
  const putNumAll: any = summation(putNumsInfoList);
  const putSumAll: any = summation(putSumsInfoList);

  return {
    topStateNameList,
    topStatePayNums,
    topStatePaySums,
    topStatePutNums,
    topStatePutSums,
    payNumsInfoList,
    paySumsInfoList,
    putNumsInfoList,
    putSumsInfoList,
    payNumAll,
    paySumAll,
    putNumAll,
    putSumAll,
  };
};

/** 处理金额和单位 */
const handleSum = (sumValue: number) => {
  let sumStr: number = sumValue ? sumValue : 0;
  let unitStr: string = '元';
  if (sumStr >= 10000) {
    sumStr = sumStr / 10000;
    unitStr = '万元';
  } else if (sumStr >= 1000000) {
    sumStr = sumStr / 1000000;
    unitStr = '百万';
  } else if (sumStr >= 1000000) {
    sumStr = sumStr / 1000000;
    unitStr = '千万';
  }

  const newSum: string = Number(sumStr)?.toFixed(4);
  return (
    <>
      <span style={{ fontSize: 16, color: '#ff4d4f', fontWeight: 600 }}>{newSum}</span>
      &nbsp;{unitStr}
    </>
  );
};

/** 处理返回字符串 */
const handleSumStr = (sumValue: number) => {
  let sumStr: number = sumValue ? sumValue : 0;
  let unitStr: string = '元';
  if (sumStr >= 10000) {
    sumStr = sumStr / 10000;
    unitStr = '万元';
  } else if (sumStr >= 1000000) {
    sumStr = sumStr / 1000000;
    unitStr = '百万';
  } else if (sumStr >= 1000000) {
    sumStr = sumStr / 1000000;
    unitStr = '千万';
  }

  const newSum: string = Number(sumStr)?.toFixed(4);

  return `${newSum} ${unitStr}`;
};

/** 处理金额从大到小，且取前10位 */
const sumSortDecs = (infoData: Record<string, any>[]) => {
  const sortList: Record<string, any>[] = infoData?.sort((a, b) => {
    return b?.totalSum - a?.totalSum;
  });
  return sortList;
};

/** 合并相对方统计数据 */
const mergeOppositeData = (dataInfo: Record<string, any>) => {
  let oppositeList: Record<string, any>[] = [];
  if (dataInfo && JSON.stringify(dataInfo) != '{}') {
    Object.values(dataInfo)?.forEach((item: any) => {
      oppositeList = oppositeList?.concat(item);
    });
  }

  /** 将数据排序，并获取前十位金额数据 */
  const sortTenList: Record<string, any>[] = sumSortDecs(oppositeList);
  return sortTenList;
};

export { stateCountContract, handleSum, mergeOppositeData, summation, handleSumStr, float_add };
